package leetcode.code;

import leetcode.IDebug;

public class Solution859 implements IDebug {

	public boolean buddyStrings(String s, String goal) {
		if (s.equals(goal)) {
			if (s.length() == 1) {
				return false;
			}
			int[] chars = new int[26];
			for (char c : goal.toCharArray()) {
				int num = ++chars[c - 'a'];
				if (num == 2) {
					return true;
				}
			}
			return false;
		} else if (s.length() == goal.length()) {
			char[] ss = s.toCharArray();
			char[] gs = goal.toCharArray();
			int mark = 0;
			char d1 = ' ';
			char d2 = ' ';
			for (int i = 0; i < s.length(); i++) {
				char c1 = ss[i];
				char g1 = gs[i];
				if (c1 != g1) {
					if (mark == 0) {
						d1 = c1;
						d2 = g1;
						mark++;
						continue;
					} else if (mark == 1) {
						if (d1 != g1 || d2 != c1) {
							return false;
						}
						mark++;
						continue;
					}
					return false;
				}
			}
			return mark == 2;
		}
		return false;
	}

	@Override
	public void debug1() {
		String s = "ab";
		String goal = "ba";
		boolean r = this.buddyStrings(s, goal);
		boolean e = true;

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution859 so = new Solution859();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
